//
//  MoreNewsViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/3.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MoreNewsViewController.h"
#import "MainTableViewCell.h"
#import "JuNengLolViewController.h"

@interface MoreNewsViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    UITableView * _tableView;
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;

}


@end

@implementation MoreNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"大事件")
     _dataArray = @[].mutableCopy;
    _page =1;
    
    [self getListData];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    [self creatTableView];
}

-(void)back{

    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getListData];
        
    }];
    _tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getListData];
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    _tableView.mj_footer.hidden = NO;

    [_tableView registerNib:[UINib nibWithNibName:[MainTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 120;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
   
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    MainTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[MainTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    [cell.newsImage sd_setImageWithURL:ImageUrl(dic[@"img_src"]) placeholderImage:ImageName(@"img_new_default")];
    cell.newsTitle.text = dic[@"title"];
    cell.timeLabel.text = dic[@"create_time"];
    cell.newsDetails.text = dic[@"brief"];
    
    cell.newsDetails.frame = CGRectMake(125, 44, WIDTH-135, 20);
    cell.newsDetails.numberOfLines = 2;
    [cell.newsDetails sizeToFit];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    JuNengLolViewController * details = [[JuNengLolViewController alloc]init];
    details.newsIdStr = dic[@"news_id"];
    details.titleStr = @"大事件";
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
    
}


-(void)getListData{
    
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",_page],
                           @"page_num":@"10"
                          };
    
    //    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_bigNewsList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [responseObject[@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    //                    noBodyView.hidden =YES;
                    
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                    
                }else {
                    _tableView.mj_footer.hidden = YES;
                    //                    noBodyView.hidden =NO;
                    //
                    //                    [self.view addSubview:noBodyView];
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}


@end
